<template>
  <div class="container">
    <div class="app-container">
      <!-- <el-tree  /> -->
      <el-tree :data="dapts" default-expand-all :expand-on-click-node="false" :props="defaultProps">

        <template #default="{data}">
          <el-row style=" width:100%; height:40px" type="flex" justify="space-between" align="middle">
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="manager-name">{{ data. managerName }}</span>
              <el-dropdown @command="opertDrop($event,data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edli">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <AddDept ref="addDeptCom" :depart-id="departId" :show-dialog.sync="showDialog" @updateDepartment="getdaps" />
  </div>
</template>
<script>
import AddDept from './components/AddDept.vue'
import { getDepartment, deleteDepartment } from '@/api/department'
import { translateListToTreeDate } from '@/utils/index'
export default {
  name: 'Department',
  components: {
    AddDept
  },
  data() {
    return {
      departId: null,
      showDialog: false,
      dapts: [
        {
          name: '传智教育',
          managerName: '管理员',
          children: [
            {
              name: '总裁办',
              managerName: '李四'

            },
            {
              name: '行政部',
              managerName: '张三'
            },
            {
              name: '人事部',
              managerName: '张三',
              children: [
                {
                  name: '财务核算部',
                  managerName: '张三'
                }

              ]
            }
          ]
        }
      ],
      defaultProps: {
        label: 'name',
        children: 'children'
      }
    }
  },
  created() {
    this.getdaps()
  },
  methods: {
    async opertDrop(type, id) {
      console.log(id)
      if (type === 'add') {
        this.departId = id
        this.showDialog = true
      } else if (type === 'edli') {
        this.departId = id
        this.showDialog = true
        // vue更新dom的过程是异步的，这样为了提高性能，一旦数据发生了变化，组件实例和dom更新渲染时异步的，所以使用$nextTick获取到更行后的dom和组件实例
        this.$nextTick(() => {
          this.$refs.addDeptCom.getDepartmentDetail()
        })
      } else {
        await this.$confirm('确认要删除此部门吗？', '温馨提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        })
        await deleteDepartment(id)
        this.$message.success('删除部门成功')
        this.getdaps()
      }
    },
    async  getdaps() {
      const res = await getDepartment()
      this.dapts = translateListToTreeDate(res, 0)
    }
  }
}
</script>
<style scoped>
.app-container{
  padding: 30px 140px;
  font-size: 14px;
}
.manager-name{
  width: 50px;
  display: inline-block;
  margin-right: 10px;

}
</style>
